# import requests
from lxml import etree
from curl_cffi import requests
import time
url = 'https://huaban.com/v3/search/file?text=%E4%B8%87%E5%9C%A3%E8%8A%82%E6%B5%B7%E6%8A%A5&sort=all&filter_ids[]=5342747,5342750,5342756&limit=40&page=1&position=search_pin&fields=pins:PIN%7Ctotal,facets,split_words,relations,rec_topic_material,topics'
headers = {
    "accept": "application/json, text/plain, */*",
    # "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "cache-control": "no-cache",
    "cookie": "user_device_id=fa8f753f67ed4a37bbddffa3ab8793ad; fd_id=ee09d4fa1ef59ab0a5d39fd7b1e050b6; fd_id_timestamp=1730297941940; _ga=GA1.1.1323406039.1734164881; gray-user.prod=1; _ga_GNS4BGH70N=GS1.1.1734188077.2.0.1734188077.0.0.0; canary_uuid=2c8214906edbb200031b47161ee4821a; user_device_id_timestamp=1761822463635; Hm_lvt_d4a0e7c3cd16eb58a65472f40e7ee543=1761822465; HMACCOUNT=DDE96B9C8A5564BC; _clck=1dkz688%5E2%5Eg0l%5E0%5E1809; canary=.?base; locale=zh-cn; token.prod.sig=LgP_dG9veunv9j0WRaRAVdTKhsZvQ6NpmOwrrCkHcFc; referer=https%3A%2F%2Fwww.gaoding.art%2F; sid=s%3AdRzbrKjPIgb8m6dgajjeHfyPPIysPHGk.7AraaZJE6LLjnt23ycto%2BROw3pYaTmjOAiueBJOjCsk; gd_id=2023156960760188003; x-product-type=INDIVIDUAL_FREE; feature_page=%257B%2522all%2522%253A4%257D; ab_test_id=48a2919aa130e37bca2adfdb86140f82; huaban-page-setting={%22cols%22:2%2C%22columnSize%22:209}; Hm_lpvt_d4a0e7c3cd16eb58a65472f40e7ee543=1761826251; _clsk=jwz0jh%5E1761826252852%5E47%5E0%5Ez.clarity.ms%2Fcollect",
    "guest-token": "fa8f753f67ed4a37bbddffa3ab8793ad",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://huaban.com/search?q=%E4%B8%87%E5%9C%A3%E8%8A%82%E6%B5%B7%E6%8A%A5&sort=all&type=pin&filter_ids=file_type-5342747.5342750.5342756",
    "sec-ch-ua": "\"Microsoft Edge\";v=\"141\", \"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"141\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
    "x-abtest-tags": "eyJpbnRlcnZlbnRpb25fcGluX2xhYiI6InN0cmluZ18wIiwic2VhcmNoX3N1cHBvcnRfc2NvcmUiOiJzdHJpbmdfNyIsInRyYWZmaWNfbWF0ZXJpYWxfbGFiIjoic3RyaW5nXzAiLCJ0cmFmZmljX3NlcmFjaF9waW5fbGFiIjoic3RyaW5nXzAiLCJtdWx0aV90YXJnZXRfbW9kYWwiOiJzdHJpbmdfMSIsImludGVydmVudGlvbl9tYXRlcmlhbF9sYWIiOiJzdHJpbmdfMCJ9",
    "x-gd-traffic-tags": "base",
    "x-guest-token": "fa8f753f67ed4a37bbddffa3ab8793ad",
    "x-product-type": "INDIVIDUAL_FREE"
}
time.sleep(1)
res = requests.get(url,headers=headers)
# print(res.status_code)
# print(res.json()['pins'])
#
# print(len(res.json()['pins']))

# "type": "image/jpeg",


for item in res.json()['pins']:
    # print(item.get('file')['key'])
    img_type = item.get('file')['type'][6:]
    title = item.get('user')['username']
    img_key = item.get('file')['key']
    # print(title,img_key,img_type)
    img_url = 'https://gd-hbimg.huaban.com/'+item.get('file')['key']
    print(title,img_url)
    req = requests.get(img_url,headers=headers)
    print(req.status_code)
    with open(f'static/花瓣/{title}.{img_type}','wb') as f:
        f.write(req.content)
        time.sleep(1)


